﻿using Kingdee.BOS;
using Kingdee.BOS.App.Core.Convertible.BusinessService;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.SerivesPlugin.BPW
{
    [Description("采购入库时，含税单不等于采购订单的含税单价，不允许保存"), HotUpdate]
    public class CheckPurPrice : AbstractOperationServicePlugIn
    {
        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);
            //if (this.Context.ServiceType != WebType.WebService) { return; }
            foreach (var obj in e.SelectedRows.Select(s => s.DataEntity))
            {
                if (obj != null)
                {
                    DynamicObjectCollection rows = obj["InStockEntry"] as DynamicObjectCollection;
                    foreach (var row in rows)
                    {
                        int purRecId = 0;
                        DynamicObjectCollection lkRows = row["FInStockEntry_Link"] as DynamicObjectCollection;
                        foreach (var lkRow in lkRows)
                        {
                            purRecId = Convert.ToInt32(lkRow["sid"]);
                        }
                        int eId = Convert.ToInt32(row["Id"]);
                        string sql = string.Format($@"/*dialect*/ SELECT T4.FENTRYID
                        FROM T_PUR_ReceiveEntry T2
                             JOIN T_PUR_RECEIVEENTRY_LK T3 ON T3.FENTRYID = T2.FENTRYID
                             JOIN t_PUR_POOrderEntry T4 ON T4.FENTRYID = T3.FSID
                        WHERE T2.FENTRYID = {purRecId} ");
                        var dbs = DBServiceHelper.ExecuteDataSet(this.Context, sql);
                        int purEid = Convert.ToInt32(row["POORDERENTRYID"]);
                        if (dbs.Tables[0].Rows.Count > 0)
                        {
                            purEid = Convert.ToInt32(dbs.Tables[0].Rows[0][0]);
                        }
                        decimal purPrice = Convert.ToDecimal(_GetPurPrice(purEid)[0][0]);
                        bool giveAway = Convert.ToBoolean(_GetPurPrice(purEid)[0][1]);
                        if (!giveAway)
                        {
                            decimal taxPrice = Convert.ToDecimal(row["TaxPrice"]);
                            if (taxPrice <= 0)
                            {
                                e.Cancel = true;
                                throw new Exception($"采购入库的含税单价:{taxPrice},为0，不允许保存");
                            }
                            if (purPrice <= 0)
                            {
                                e.Cancel = true;
                                throw new Exception($"采购订单的含税单价:{purPrice},为0，不允许保存");
                            }
                            if (purPrice != taxPrice)
                            {
                                e.Cancel = true;
                                throw new Exception($"采购入库的含税单价:{taxPrice},与采购订单的含税单价:{purPrice},不一样，不允许保存");
                            }
                        }
                    }
                }
            }
        }

        private List<List<object>> _GetPurPrice(int purEid)
        {
            //decimal purPrice = 0;
            var res = Api.ExecuteBillQuery(this.Context, "PUR_PurchaseOrder", "FTaxPrice,FGiveAway", $"FPOOrderEntry_FEntryID={purEid}", "FTaxPrice", 1);
            //if (res.Count > 0)
            //{
            //    purPrice = Convert.ToDecimal(res.FirstOrDefault().FirstOrDefault());
            //}
            return res;
        }
    }
}
